package com.zhihuiyanche.controller.orders;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.zhihuiyanche.domain.ListResult;
import com.zhihuiyanche.domain.Orders;
import com.zhihuiyanche.service.orders.OrdersService;

@RestController
public class OrdersController
{
	@Autowired
	private OrdersService	ordersService;

	/**
	 * 查询列表
	 * 
	 * @param req
	 * @param resp
	 * @param context
	 * @return
	 */
	@RequestMapping(value = "toOrderListPage", method ={ RequestMethod.POST, RequestMethod.GET })
	public ModelAndView toOrderListPage(HttpServletRequest req, HttpServletResponse resp, ModelMap context)
	{
		ModelAndView mv = new ModelAndView("tables");
		return mv;
	}

	/**
	 * 查询列表
	 * 
	 * @param req
	 * @param resp
	 * @param context
	 * @return
	 */
	@RequestMapping(value = "getOrdersByCondition", produces = "application/json; charset=utf-8", method ={ RequestMethod.GET})
	public @ResponseBody String getOrdersByCondition(HttpServletRequest req, HttpServletResponse resp, HttpSession session)
	{
		ListResult result = new ListResult();
		result.setSuccess(true);
		try
		{
			Map<String, String[]> param = req.getParameterMap();
			
			JSONObject orders = new JSONObject();
			
			for(String key : param.keySet())
			{
				orders.put(key, param.get(key)[0]);
			}
			
			String shopId = (String) session.getAttribute("shopId");
			orders.put("shopId",shopId);
			JSONArray ordersJsonObj = ordersService.getOrdersByCondition(orders);
			
			result.setRows(ordersJsonObj);
			result.setTotal(ordersService.getOrdersByPageCount(orders));
		}
		catch(Exception e)
		{
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		
		
		return JSONObject.fromObject(result).toString();
	}
	
	
	/**
	 * 查询新订单的数量，可通过轮询的方式
	 * 
	 * @param req
	 * @param resp
	 * @param context
	 * @return
	 */
	@RequestMapping(value = "getOrdersCountByCondition", produces = "application/json; charset=utf-8", method ={ RequestMethod.GET})
	public @ResponseBody String getOrdersCountByCondition(HttpServletRequest req, HttpServletResponse resp, HttpSession session)
	{
		ListResult result = new ListResult();
		result.setSuccess(true);
		try
		{
			Map<String, String[]> param = req.getParameterMap();
			
			JSONObject orders = new JSONObject();
			
			for(String key : param.keySet())
			{
				orders.put(key, param.get(key)[0]);
			}
			
			String shopId = (String) session.getAttribute("shopId");
			orders.put("shopId",shopId);
			
			result.setTotal(ordersService.getOrdersByPageCount(orders));
		}
		catch(Exception e)
		{
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		
		
		return JSONObject.fromObject(result).toString();
	}
	
	

	/**
	 * 查询列表
	 * 
	 * @param req
	 * @param resp
	 * @param context
	 * @return
	 */
	@RequestMapping(value = "saveOrdersSuccess", produces = "application/json; charset=utf-8", method =
	{ RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody String saveOrdersSuccess(Orders orders, HttpServletRequest req, HttpServletResponse resp, ModelMap context)
	{
		boolean tf = ordersService.saveOrdersSuccess(orders);
		String result = "";
		if (tf)
		{
			result = "1";
		}
		else
		{
			result = "0";
		}
		return result;
	}
	
	/**
	 * 查询列表
	 * 
	 * @param req
	 * @param resp
	 * @param context
	 * @return
	 */
	@RequestMapping(value = "getOrdersChart", produces = "application/json; charset=utf-8", method =
		{ RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody String getOrdersChart(HttpServletRequest req, HttpServletResponse resp, HttpSession session, ModelMap context)
	{
		String shopId = (String)session.getAttribute("shopId");
		List<String[]> datas = ordersService.getOrdersChart(shopId);
		return JSONArray.fromObject(datas).toString();
	}

	public void setOrdersService(OrdersService ordersService)
	{
		this.ordersService = ordersService;
	}

}